package com.ruoyi.exen.common.utils.email;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Value;

/**
 * 发送邮箱工具类
 * @author EMY2305005
 */
public class EmailUtils {

    @Value("${email.sender.username}")
    public static String senderUsername;

    @Value("${email.sender.password}")
    public static String senderPassword;

    @Value("${email.hostname}")
    public static String hostname;

    @Value("${email.smtpPort}")
    public static int smtpPort;


    /**
     * 发送邮件给供应商，让其尽快提交文件
     */
    public static void emailToVendor(String addressee,String topic,String msg){
        emailSender(addressee,topic,msg);
    }

    /**
     *  发送邮件
     * @param to 收件人
     * @param topic 标题
     * @param msg 内容
     */
    public static void emailSender(String to,String topic, String msg) {
        try {
            // 创建Email对象
            Email email = new SimpleEmail();

            // 配置SMTP服务器设置
            email.setHostName(hostname); // 设置SMTP服务器地址
            email.setSmtpPort(smtpPort); // 设置SMTP服务器端口号
            email.setSSLOnConnect(true);
            email.setAuthenticator(new DefaultAuthenticator(senderUsername,senderPassword )); // 设置发件人邮箱和密码
            //email.setStartTLSRequired(true); // 启用TLS加密
            email.setFrom(senderUsername); // 设置发件人邮箱地址
            email.setSubject(topic); // 设置邮件主题
            email.setMsg(msg); // 设置邮件内容
            email.addTo(to); // 设置收件人邮箱地址
            // 发送邮件
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

}
